<html>
<?php
ob_start();
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="matematrix_db"; // Database name
$tbl_name="utilizadores_tbl"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$username=$_POST['username'];
$password=$_POST['password'];

// To protect MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);


// encrypt password
//$mypassword=md5($mypassword);

$sql="SELECT * FROM $tbl_name WHERE utilizador='$username' and password='$password' AND tipo > 0";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
//session_register("myusername");
//session_register("mypassword");
$dados = @mysql_fetch_array($result);
$_SESSION["id"] = ($dados["id"]);
$_SESSION["username"] = ($dados["utilizador"]);
$_SESSION["pass"] = ($dados["password"]);
$_SESSION["profile"] = ($dados["tipo"]);

header("location:../index.php");
}
else {
	echo "<p>Utilizador ou Palavra-Chave errados. \n </p>";
	echo "<p>Será redirecionado dentro de 2 segundos... Caso contrário, carregue <a href='../index.php'>AQUI</a></p>";
	header("Refresh: 2; url=../index.php");
}

ob_end_flush();
?>
<body>
</body>
</html>